﻿using GWSoft.Clark.EventBus.Common.Events;
using GWSoft.Clark.EventBus.Vehicle.Events;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace GWSoft.Clark.EventBus.Vehicle.EventHandlers
{
    public class VehicleCreatedEventHandler : IEventHandler<VehicleCreatedEvent>
    {
        private readonly ILogger logger;

        public VehicleCreatedEventHandler(ILogger<VehicleCreatedEventHandler> logger)
        {
            this.logger = logger;
            this.logger.LogInformation($"车辆模块 VehicleCreatedEventHandler构造函数调用完成。Hash Code: {this.GetHashCode()}.");
        }

        public bool CanHandle(IEvent @event)
            => @event.GetType().Equals(typeof(VehicleCreatedEvent));

        public Task<bool> HandleAsync(VehicleCreatedEvent @event, CancellationToken cancellationToken = default)
        {
            this.logger.LogInformation($"车辆模块开始处理VehicleCreatedEventHandler事件，处理器Hash Code：{this.GetHashCode()}.");
            //车辆功能中的处理
            return Task.FromResult(true);
        }

        public Task<bool> HandleAsync(IEvent @event, CancellationToken cancellationToken = default)
        {
            return CanHandle(@event) ? HandleAsync((VehicleCreatedEvent)@event, cancellationToken) : Task.FromResult(false);
        }
    }
}
